package cn.fintecher.pangolin.service.business.model.request;

import cn.fintecher.pangolin.common.utils.ZWStringUtils;
import cn.fintecher.pangolin.entity.mysql.QCaseInfo;
import com.querydsl.core.BooleanBuilder;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;
import java.util.Objects;

import static org.elasticsearch.index.query.QueryBuilders.rangeQuery;

@Data
public class PaymentRecordRequest {

    @ApiModelProperty("委托方id标")
    private String principalId;

    @ApiModelProperty("姓名")
    private String personalName;

    @ApiModelProperty("证件")
    private String certificateNo;

    @ApiModelProperty("批次")
    private String batchNumber;

    @ApiModelProperty("卡号")
    private String cardNo;

    @ApiModelProperty("手次")
    private String stage;

    @ApiModelProperty(value = "催收员")
    private String collectorUsername;

    @ApiModelProperty(value = "部门code")
    private String departCode;

    @ApiModelProperty("还款日期开始")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date payDateStart;

    @ApiModelProperty("还款日期结束")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date payDateEnd;

    @ApiModelProperty("还款金额开始")
    private Double payAmtStart;

    @ApiModelProperty("还款金额结束")
    private Double payAmtEnd;

    @ApiModelProperty("案件编号")
    private String caseNumber;

    public BooleanBuilder generateBooleanBuilder() {
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        QCaseInfo qCaseInfo = QCaseInfo.caseInfo;
        if (ZWStringUtils.isNotEmpty(this.principalId)) {
            booleanBuilder.and(qCaseInfo.principalId.eq(this.principalId));
        }
        if (ZWStringUtils.isNotEmpty(this.personalName)) {
            booleanBuilder.and(qCaseInfo.personalName.like(StringUtils.join(this.personalName, "%")));
        }
        if (ZWStringUtils.isNotEmpty(this.collectorUsername)) {
            booleanBuilder.and(qCaseInfo.currentCollector.realName.like(StringUtils.join(this.collectorUsername, "%")));
        }
        if (ZWStringUtils.isNotEmpty(this.departCode)) {
            booleanBuilder.and(qCaseInfo.currentCollector.department.departCode.like(StringUtils.join(this.departCode, "%")))
                    .or(qCaseInfo.department.departCode.like(StringUtils.join(this.departCode, "%")));
        }
        if (ZWStringUtils.isNotEmpty(this.certificateNo)) {
            booleanBuilder.and(qCaseInfo.certificateNo.like(StringUtils.join(this.certificateNo, "%")));
        }
        if (ZWStringUtils.isNotEmpty(this.cardNo)) {
            booleanBuilder.and(qCaseInfo.cardNo.like(StringUtils.join(this.cardNo, "%")));
        }
        if (ZWStringUtils.isNotEmpty(this.batchNumber)) {
            booleanBuilder.and(qCaseInfo.batchNumber.like(StringUtils.join(this.batchNumber, "%")));
        }
        if (ZWStringUtils.isNotEmpty(this.stage)) {
            booleanBuilder.and(qCaseInfo.stage.eq(this.stage));
        }
        if (ZWStringUtils.isNotEmpty(this.caseNumber)) {
            booleanBuilder.and(qCaseInfo.caseNumber.eq(this.caseNumber));
        }
        return booleanBuilder;
    }

    public BoolQueryBuilder generateBillQueryBuilder() {
        BoolQueryBuilder qb = QueryBuilders.boolQuery();
        qb.must(QueryBuilders.matchPhraseQuery("flag", 0));
        if (Objects.nonNull(this.payAmtStart)) {
            qb.must(rangeQuery("paymentAmt").gte(this.payAmtStart));
        }
        if (Objects.nonNull(this.payAmtEnd)) {
            qb.must(rangeQuery("paymentAmt").lte(this.payAmtEnd));
        }
        if (Objects.nonNull(this.payDateStart)) {
            qb.must(rangeQuery("paymentDate").gte(this.payDateStart.getTime()));
        }
        if (Objects.nonNull(this.payDateEnd)) {
            qb.must(rangeQuery("paymentDate").lte(this.payDateEnd.getTime() + 86400000));
        }
        return qb;
    }
}
